package com.example.administrator.zhjt.util;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public class MyVolley {
    public static final String IP = "192.168.1.101";
    public static final String PORT = "8080";
    public static final String GET_ROAD_STATUS = "http://" + IP + ":" + PORT + "/transportservice/action/GetRoadStatus.do";
    public static final String GET_BUS_STATIONINFO="http://"+IP+":"+PORT+"/transportservice/action/GetBusStationInfo.do";
    private static RequestQueue requestQueue;

    public static void init(Context context){
        requestQueue= Volley.newRequestQueue(context);
        requestQueue.start();
    }

    public interface OnResultListener{
        void done(JSONObject result);
        void fail(VolleyError error);
    }

    public static void Post(String url, JSONObject param, final OnResultListener listener){
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url, param, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject result) {
                listener.done(result);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.fail(volleyError);
            }
        });
        requestQueue.add(jsonObjectRequest);
    }


    public static void getRoadStatus(int roadId,String username ,final OnResultListener listener){
        Map map = new HashMap();
        map.put("RoadId",roadId);
        map.put("UserName",username);

        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, GET_ROAD_STATUS, new JSONObject(map), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject result) {
                listener.done(result);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.fail(volleyError);
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public static void getBusStationinfo(int BusStationId,final OnResultListener listener){
        Map map=new HashMap();
        map.put("BusStationId",BusStationId);

        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, GET_BUS_STATIONINFO, new JSONObject(map), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                listener.done(jsonObject);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                listener.fail(volleyError);
            }
        });
        requestQueue.add(jsonObjectRequest);

    }

}
